﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class Police_TranslateApprove_FlowSeting : System.Web.UI.Page
{
    DataTable ListItemName = null;
   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        //流程类型
        this.DptFlowType.DataSource = Yipusoft.TranslateApprove.BLL.Kinds.GetList();
        this.DptFlowType.DataTextField = "KName";
        this.DptFlowType.DataValueField = "Kind";
        this.DptFlowType.DataBind();

        //获取角色
        this.DltRoles.DataSource = Yipusoft.TranslateApprove.BLL.Roles.GetList();
        this.DltRoles.DataBind();

        //排序
        for (int i = 0; i <= 20; i++)
        {
            this.DptOrderID.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

        ListItemName = new DataTable();
        DataColumn CID = new DataColumn("ID", Type.GetType("System.String"));
        DataColumn CName = new DataColumn("CName", Type.GetType("System.String"));
        ListItemName.Columns.Add(CID);
        ListItemName.Columns.Add(CName);
        ViewState["List"] = ListItemName;
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        lblPromt.Text = "";
        if (Yipusoft.TranslateApprove.BLL.Process.GetCount("KID=" + DptFlowType.SelectedValue + "") > 0)
        {
            lblPromt.Text = "操作失败：该流程类型操作步骤已存";
            return;
        }
        if (ViewState["List"] == null)
            return;
                

        DataTable dtb = ViewState["List"] as DataTable;
        for (int i = 0; i < dtb.Rows.Count; i++)
        {
            Yipusoft.TranslateApprove.Model.ProcessInfo info = new Yipusoft.TranslateApprove.Model.ProcessInfo();
            info.OrderID = i;
            info.JID = int.Parse((GvwList.Rows[i].FindControl("LRBtn") as RadioButtonList).SelectedItem.Value);
            info.IsJump = (GvwList.Rows[i].FindControl("CBoxSkip") as CheckBox).Checked;
            info.PName = txtFlow.Text.Trim();
            info.RID = int.Parse(dtb.Rows[i]["ID"].ToString());
            info.Rank = ((i + 1) == dtb.Rows.Count) ? 0 : i;
            info.KID = int.Parse(DptFlowType.SelectedValue.ToString());

            if (Yipusoft.TranslateApprove.BLL.Process.Add(info) > 0)
                lblPromt.Text = "保存成功";


        }

    }
    protected void BtnRole_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument == null)
            return;

        if (ViewState["List"] == null)
            return;

        ListItemName = ViewState["List"] as DataTable;
        DataRow drw = ListItemName.NewRow();
        drw[0] = e.CommandArgument.ToString();
        drw[1] = ((LinkButton)sender).Text;
        this.ListItemName.Rows.Add(drw);

        ViewState["List"] = ListItemName;
        BindGvw();

        BtnAdd.Enabled = true;
    }

    private void BindGvw()
    {
        if (ViewState["List"] == null)
            return;
        ListItemName = ViewState["List"] as DataTable;        
        this.GvwList.DataSource = ListItemName;
        this.GvwList.DataBind();

    }
    protected void GvwList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        RadioButtonList RList = e.Row.FindControl("LRBtn") as RadioButtonList;
        if (RList == null)
            return;

        RList.DataSource= Yipusoft.TranslateApprove.BLL.JoinType.GetList(int.Parse(this.DptFlowType.SelectedValue));
        RList.DataTextField = "TypeName";
        RList.DataValueField = "ID";
        RList.DataBind();

        if (RList.Items.Count > 0)
            RList.Items[0].Selected = true;

    }
    protected void BtnClaer_Click(object sender, EventArgs e)
    {
        ListItemName = ViewState["List"] as DataTable;
        ListItemName.Clear();
        ViewState["List"] = ListItemName;
        BindGvw();
        this.BtnAdd.Enabled = false;
    }
}
